using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using SuperTicTacToe.Resources;
using Services.Menu;
using Utilities;

namespace SuperTicTacToe.Menu
{
    
    public class Player
    {
        public bool isHuman;
        public String name;
        public int color;
        public int symbol;
        public Player()
        {
            isHuman = false;
            name = "";
            color = 0;
            symbol = 0;
        }
    }

    class ConfigMenu : DrawableGameComponent
    {
        public static Color color1 = Color.Green;
        public static Color color2 = Color.Red;
        public static Color color3 = Color.Blue;
        //public static Color color4 = Color.Black;
        
        InputManager input_manager;
        Texture2D screen_bg, whiteTexture;
        //SpriteFont font, font_t, font_p;
        MyButton exit, title, start;
        MyButton prev2d, prev3d;
        MyButton title_2d, title_3d;
        SpriteBatch spriteBatch;
        StateManager stateManager;
        protected float press_wait;
        //MyButton prev_size, next_size, title_size, text_size;
        MyButton prev_players, next_players, title_players, text_players;
        protected readonly float PRESS_INTERVAL = 0.5f;
        int current_size;
        int current_players;
        ArrayList<Player> players;
        MyButton p1type, p1color, p1name;
        MyButton p2type, p2color, p2name;
        MyButton p3type, p3color, p3name;
        //MyButton p4type, p4color, p4name;
        Texture2D cpu_t, human_t;
        Texture2D cross_t, circle_t;
        public ConfigMenu(Game game, StateManager stateManager)
            : base(game)
        {
            this.stateManager = stateManager;
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            //stateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        bool is2DSelected;
        protected override void LoadContent()
        {
            //buttonbg = Game.Content.Load<Texture2D>("Levels/whiteTexture");
            
            var font = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            var font_p = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            var font_t = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            screen_bg = Game.Content.Load<Texture2D>("Textures/LevelBackground");
            var prev3d_t = Game.Content.Load<Texture2D>("Textures/3d");
            var prev2d_t = Game.Content.Load<Texture2D>("Textures/2d");
            var whiteBox = Game.Content.Load<Texture2D>("Textures/bar_box");
            whiteTexture = Game.Content.Load<Texture2D>("Textures/WhitePixel");
            var prev_t = Game.Content.Load<Texture2D>("Textures/prev");
            var next_t = Game.Content.Load<Texture2D>("Textures/next");
            cpu_t = Game.Content.Load<Texture2D>("Textures/cpu");
            human_t = Game.Content.Load<Texture2D>("Textures/human");
            cross_t = Game.Content.Load<Texture2D>("Textures/cross");
            circle_t = Game.Content.Load<Texture2D>("Textures/circle");
            Color textColor = new Color(8, 141, 217);

            exit = new MyButton(new Rectangle(0, 720, 240, 80), Strings.Menu_Back, textColor, font);
            start = new MyButton(new Rectangle(240, 720, 240, 80), Strings.Menu_Start, textColor, font);

            title = new MyButton(new Rectangle(20, 0, 440, 80), Strings.Menu_NewGame_Title, textColor, font_t);

            prev2d = new MyButton(new Rectangle(50, 90, 150, 150), prev2d_t, Color.White);
            prev3d = new MyButton(new Rectangle(280, 90, 150, 150), prev3d_t, Color.White);

            title_2d = new MyButton(new Rectangle(50, 240, 150, 40), Strings.Classic, textColor, font_p);
            title_3d = new MyButton(new Rectangle(280, 240, 150, 40), "3 D", textColor, font_p);

            //title_size = new MyButton(new Rectangle(0, 300, 200, 40), Strings.DeckSize, textColor, font_p);

            //prev_size = new MyButton(new Rectangle(200, title_size.rectangle.Y, 60, 40), prev_t, Color.White);
            //text_size = new MyButton(new Rectangle(295, title_size.rectangle.Y, 70, 40), "", textColor, font_p);
            //next_size = new MyButton(new Rectangle(400, title_size.rectangle.Y, 60, 40), next_t, Color.White);

            title_players = new MyButton(new Rectangle(0, 370, 200, 40), Strings.Players, textColor, font_p);

            prev_players = new MyButton(new Rectangle(200, title_players.rectangle.Y, 60, 40), prev_t, Color.White);
            text_players = new MyButton(new Rectangle(295, title_players.rectangle.Y, 70, 40), "", textColor, font_p);
            next_players = new MyButton(new Rectangle(400, title_players.rectangle.Y, 60, 40), next_t, Color.White);

            var y = 450;
            int type_x = 40;
            int color_x = 140;
            int name_x = 250;
            int height = 40;
            p1type = new MyButton(new Rectangle(type_x, y, height, height), human_t, Color.White);
            p1color = new MyButton(new Rectangle(color_x, y, height, height), cross_t, color1);
            p1name = new MyButton(new Rectangle(name_x, y, 200, height), "",textColor, font_p, TextPos.Center, whiteBox, textColor);

            y = 520;
            p2type = new MyButton(new Rectangle(type_x, y, height, height), human_t, Color.White);
            p2color = new MyButton(new Rectangle(color_x, y, height, height), cross_t, color2);
            p2name = new MyButton(new Rectangle(name_x, y, 200, height), "", textColor, font_p, TextPos.Center, whiteBox, textColor);

            y = 590;
            p3type = new MyButton(new Rectangle(type_x, y, height, height), human_t, Color.White);
            p3color = new MyButton(new Rectangle(color_x, y, height, height), cross_t, color3);
            p3name = new MyButton(new Rectangle(name_x, y, 200, height), "", textColor, font_p, TextPos.Center, whiteBox, textColor);

            //y = 660;
            //p4type = new MyButton(new Rectangle(type_x, y, height, height), cpu_t, Color.White);
            //p4color = new MyButton(new Rectangle(color_x, y, height, height), whiteTexture, color4);
            //p4name = new MyButton(new Rectangle(name_x, y, 200, height), "", textColor, font_p, TextPos.Center, whiteBox, textColor);

            is2DSelected = true;
            players = new ArrayList<Player>();
            players.Add(new Player() { name = "", isHuman = true, color = 0, symbol=0 });
            players.Add(new Player() { name = "", isHuman = true, color = 1, symbol=0 });
            players.Add(new Player() { name = "", isHuman = true, color = 2, symbol=0 });
            //players.Add(new Player() { name = "", isHuman = false, color = 3 });
            current_players = 2;
            current_size = 0;
            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            Game.Components.Remove(input_manager);
            base.Dispose(disposing);
        }

        public void DisposeMenu()
        {
            Game.Services.RemoveService(typeof(IInputManager));
            Game.Components.Remove(input_manager);
            this.Dispose();
        }
        public override void Update(GameTime gameTime)
        {
            if (press_wait < PRESS_INTERVAL)
                press_wait += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (current_players <= 2)
                prev_players.enabled = false;
            else
                prev_players.enabled = true;
            if (current_players < 3 && !is2DSelected)
                next_players.enabled = true;
            else
                next_players.enabled = false;
            //if (current_players < 4)
            //{
            //    p4color.enabled = false;
            //    p4name.enabled = false;
            //    p4type.enabled = false;
            //}
            //else
            //{
            //    p4color.enabled = true;
            //    p4name.enabled = true;
            //    p4type.enabled = true;
            //}
            if (current_players < 3)
            {
                p3color.enabled = false;
                p3name.enabled = false;
                p3type.enabled = false;
            }
            else
            {
                p3color.enabled = true;
                p3name.enabled = true;
                p3type.enabled = true;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                stateManager.SelectMainMenu();
            }
            base.Update(gameTime);
        }

        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            spriteBatch.Draw(screen_bg, bg_rect, Color.White);

            title.Draw(spriteBatch);

            exit.Draw(spriteBatch);
            start.Draw(spriteBatch);

            prev2d.Draw(spriteBatch);
            prev3d.Draw(spriteBatch);
            if (is2DSelected)
                spriteBatch.Draw(whiteTexture, prev3d.rectangle, Color.Black * 0.8f);
            else
                spriteBatch.Draw(whiteTexture, prev2d.rectangle, Color.Black * 0.8f);
            title_2d.Draw(spriteBatch);
            title_3d.Draw(spriteBatch);

            title_players.Draw(spriteBatch);
            
            prev_players.Draw(spriteBatch);
            next_players.Draw(spriteBatch);
            

            text_players.text = "" + current_players;
            text_players.UpdateTextPos();
            text_players.Draw(spriteBatch);

            
            //title_size.Draw(spriteBatch);
            //prev_size.Draw(spriteBatch);
            //next_size.Draw(spriteBatch);
            //text_size.text = current_size == 0 ? Strings.Small : current_size == 1 ? Strings.Medium : Strings.Big;
            //text_size.UpdateTextPos();
            //text_size.Draw(spriteBatch);
            


            p1color.Draw(spriteBatch);
            p1name.Draw(spriteBatch);
            p1type.Draw(spriteBatch);

            p2color.Draw(spriteBatch);
            p2name.Draw(spriteBatch);
            p2type.Draw(spriteBatch);

            //if (current_players > 2)
            //{
            p3color.Draw(spriteBatch);
            p3name.Draw(spriteBatch);
            p3type.Draw(spriteBatch);
            //}

            //if (current_players > 3)
            //{
            //p4color.Draw(spriteBatch);
            //p4name.Draw(spriteBatch);
            //p4type.Draw(spriteBatch);
            //}

            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;

            if (exit.tapped_on(tp))
                stateManager.SelectMainMenu();
            else if (prev2d.tapped_on(tp) && !is2DSelected)
            {
                is2DSelected = true;
                current_players = 2;
            }
            else if (prev3d.tapped_on(tp) && is2DSelected)
                is2DSelected = false;
            //else if (next_size.tapped_on(tp))
            //{
            //    current_size++;
            //    current_size %= 3;
            //}
            //else if (prev_size.tapped_on(tp))
            //{
            //    current_size--;
            //    if (current_size < 0) current_size = 2;
            //}
            else if (prev_players.tapped_on(tp))
            {
                current_players--;
                if (current_players < 2)
                    current_players = 2;
            }
            else if (next_players.tapped_on(tp))
            {
                current_players++;
                if (current_players > 4)
                    current_players = 4;
            }

            else if (p1color.tapped_on(tp))
            {
                if (players[0].symbol == 0)
                {
                    players[0].symbol = 1;
                    p1color.texture = circle_t;
                }
                else
                {
                    players[0].symbol = 0;
                    p1color.texture = cross_t;
                }

            }
            else if (p2color.tapped_on(tp))
            {
                if (players[1].symbol == 0)
                {
                    players[1].symbol = 1;
                    p1color.texture = circle_t;
                }
                else
                {
                    players[1].symbol = 0;
                    p1color.texture = cross_t;
                }

            }
            else if (p3color.tapped_on(tp))
            {
                if (players[2].symbol == 0)
                {
                    players[2].symbol = 1;
                    p1color.texture = circle_t;
                }
                else
                {
                    players[2].symbol = 0;
                    p1color.texture = cross_t;
                }

            }
            //else if (p1type.tapped_on(tp))
            //{
            //    players[0].isHuman = !players[0].isHuman;
            //    if (players[0].isHuman)
            //        p1type.texture = human_t;
            //    else
            //        p1type.texture = cpu_t;
            //}
            //else if (p2type.tapped_on(tp))
            //{
            //    players[1].isHuman = !players[1].isHuman;
            //    if (players[1].isHuman)
            //        p2type.texture = human_t;
            //    else
            //        p2type.texture = cpu_t;
            //}
            //else if (p3type.tapped_on(tp))
            //{
            //    players[2].isHuman = !players[2].isHuman;
            //    if (players[2].isHuman)
            //        p3type.texture = human_t;
            //    else
            //        p3type.texture = cpu_t;
            //}
            //else if (p4type.tapped_on(tp))
            //{
            //    players[3].isHuman = !players[3].isHuman;
            //    if (players[3].isHuman)
            //        p4type.texture = human_t;
            //    else
            //        p4type.texture = cpu_t;
            //}
            else if (p1name.tapped_on(tp))
                OpenKeyboard(p1name);
            else if (p2name.tapped_on(tp))
                OpenKeyboard(p2name);
            else if (p3name.tapped_on(tp))
                OpenKeyboard(p3name);
            //else if (p4name.tapped_on(tp))
            //    OpenKeyboard(p4name);
            else if (start.tapped_on(tp))
            {
#if WINDOWS
                for (int i = 0; i < current_players; i++)
                    players[i].name = "Player " + i;
#endif
                for (int i = 0; i < current_players; i++)
                    if (players[i].name.Equals(""))
                        return;
                ArrayList<Player> p = new ArrayList<Player>();
                for (int i = 0; i < current_players; i++)
                    p.Add(players[i]);
                if (is2DSelected)
                    stateManager.StartGame2D(p, current_size);
                else
                    stateManager.StartGame3D(p, current_size);
            }
        }
        MyButton activeTextArea = null;
        private void OpenKeyboard(MyButton area)
        {
            if (!Guide.IsVisible)
            {
                Guide.BeginShowKeyboardInput(
                  PlayerIndex.One,
                  Strings.InsertName,
                  "",
                  area.text,
                  new AsyncCallback(OnEndShowKeyboardInput),
                  null
                    );
                activeTextArea = area;
            }
        }
        private void OnEndShowKeyboardInput(IAsyncResult result)
        {
            activeTextArea.text = Guide.EndShowKeyboardInput(result);
            if (activeTextArea.text == null)
                activeTextArea.text = "";
            activeTextArea.UpdateTextPos();
            activeTextArea = null;
        }

    }
}
